package city.spring.modules.personnel.service.impl;

import city.spring.modules.personnel.entity.EmployeeRewardPunishmentEntity;
import city.spring.modules.personnel.repository.EmployeeRewardPunishmentRepository;
import city.spring.modules.personnel.service.EmployeeRewardPunishmentService;
import city.spring.modules.personnel.vo.EmployeeRewardPunishmentEntityVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.List;

/**
 * Service：员工奖惩
 *
 * @author 潘小梅
 * @date 2020-04-08 15:12:03
 */
@CacheConfig(cacheNames = {EmployeeRewardPunishmentServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class EmployeeRewardPunishmentServiceImpl extends ServiceImpl<EmployeeRewardPunishmentRepository, EmployeeRewardPunishmentEntity> implements EmployeeRewardPunishmentService {
    private final UserService userService;

    public EmployeeRewardPunishmentServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void saveEmployeeRewardPunishment(EmployeeRewardPunishmentEntity entity, @NonNull Principal user) {
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setOperatorId(userEntity.getId());
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateEmployeeRewardPunishment(EmployeeRewardPunishmentEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteEmployeeRewardPunishment(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteEmployeeRewardPunishment(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }


    @Override
    public IPage<EmployeeRewardPunishmentEntityVO> listPage(Page<? extends EmployeeRewardPunishmentEntity> page, Wrapper<? extends EmployeeRewardPunishmentEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}